package org.example.liaoxuefeng.tongyilingma;

/**
 * @author: 默苍璃
 * @date: 2024-07-17 17:32
 */
import java.util.ArrayList;
import java.util.List;

public class CommandHistory {
    private List<Command> history = new ArrayList<>();
    private int current = -1;

    public void push(Command command) {
        if (current < history.size() - 1) {
            history.subList(current + 1, history.size()).clear();
        }
        history.add(command);
        current++;
        command.execute();
    }

    public void undo() {
        if (current >= 0) {
            history.get(current).undo();
            current--;
        }
    }

    public void redo() {
        if (current < history.size() - 1) {
            current++;
            history.get(current).execute();
        }
    }
}
